⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mediaplaybackactivity.java

📁 Android平台上的media player, iPhone风格
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.android.imusic;import android.app.Activity;import android.app.AlertDialog;import android.content.BroadcastReceiver;import android.content.ComponentName;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.IntentFilter;import android.content.ServiceConnection;import android.content.pm.ActivityInfo;import android.content.pm.PackageManager;import android.content.pm.PackageManager.NameNotFoundException;import android.graphics.Bitmap;import android.media.AudioManager;import android.media.MediaFile;import android.net.Uri;import android.os.Bundle;import android.os.RemoteException;import android.os.Handler;import android.os.IBinder;import android.os.Looper;import android.os.Message;import android.os.SystemClock;import android.util.Log;import android.view.ContextMenu;import android.view.KeyEvent;import android.view.Menu;import android.view.MenuItem;import android.view.MotionEvent;import android.view.SubMenu;import android.view.View;import android.view.Window;import android.view.ContextMenu.ContextMenuInfo;import android.widget.ImageButton;import android.widget.ProgressBar;import android.widget.SeekBar;import android.widget.TextView;import android.widget.Toast;import android.widget.SeekBar.OnSeekBarChangeListener;public class MediaPlaybackActivity extends Activity implements MusicUtils.Defs, View.OnTouchListener{    private static final int USE_AS_RINGTONE = CHILD_MENU_BASE;        private boolean mOneShot = false;    private boolean mSeeking = false;    private boolean mTrackball;    private long mStartSeekPos = 0;    private long mLastSeekEventTime;    private IMediaPlaybackService mService = null;    private RepeatingImageButton mPrevButton;    private ImageButton mPauseButton;    private RepeatingImageButton mNextButton;    private ImageButton mRepeatButton;    private ImageButton mShuffleButton;    private ImageButton mQueueButton;    private Worker mAlbumArtWorker;    private AlbumArtHandler mAlbumArtHandler;    private Toast mToast;    private boolean mRelaunchAfterConfigChange;    public MediaPlaybackActivity()    {    }    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle icicle)    {        super.onCreate(icicle);        setVolumeControlStream(AudioManager.STREAM_MUSIC);        mAlbumArtWorker = new Worker("album art worker");        mAlbumArtHandler = new AlbumArtHandler(mAlbumArtWorker.getLooper());        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.audio_player);        mCurrentTime = (TextView) findViewById(R.id.currenttime);        mTotalTime = (TextView) findViewById(R.id.totaltime);        mProgress = (ProgressBar) findViewById(android.R.id.progress);        mAlbum = (AlbumView) findViewById(R.id.album);        mArtistName = (TextView) findViewById(R.id.artistname);        mAlbumName = (TextView) findViewById(R.id.albumname);        mTrackName = (TextView) findViewById(R.id.trackname);        View v = (View)mArtistName.getParent();         v.setOnTouchListener(this);        registerForContextMenu(v);        v = (View)mAlbumName.getParent();        v.setOnTouchListener(this);        registerForContextMenu(v);        v = (View)mTrackName.getParent();        v.setOnTouchListener(this);        registerForContextMenu(v);                mPrevButton = (RepeatingImageButton) findViewById(R.id.prev);        mPrevButton.setOnClickListener(mPrevListener);        mPrevButton.setRepeatListener(mRewListener, 260);        mPauseButton = (ImageButton) findViewById(R.id.pause);        mPauseButton.requestFocus();        mPauseButton.setOnClickListener(mPauseListener);        mNextButton = (RepeatingImageButton) findViewById(R.id.next);        mNextButton.setOnClickListener(mNextListener);        mNextButton.setRepeatListener(mFfwdListener, 260);        seekmethod = 1;        mTrackball = true; /* (See bug 1044348) (getResources().getConfiguration().navigation ==             Resources.Configuration.NAVIGATION_TRACKBALL);*/                mQueueButton = (ImageButton) findViewById(R.id.curplaylist);        mQueueButton.setOnClickListener(mQueueListener);        mShuffleButton = ((ImageButton) findViewById(R.id.shuffle));        mShuffleButton.setOnClickListener(mShuffleListener);        mRepeatButton = ((ImageButton) findViewById(R.id.repeat));        mRepeatButton.setOnClickListener(mRepeatListener);                if (mProgress instanceof SeekBar) {            SeekBar seeker = (SeekBar) mProgress;            seeker.setOnSeekBarChangeListener(mSeekListener);        }        mProgress.setMax(1000);                if (icicle != null) {            mRelaunchAfterConfigChange = icicle.getBoolean("configchange");            mOneShot = icicle.getBoolean("oneshot");        } else {            mOneShot = getIntent().getBooleanExtra("oneshot", false);        }    }        public boolean onTouch(View v, MotionEvent event) {        int action = event.getAction();        if (action == MotionEvent.ACTION_DOWN) {            v.setBackgroundColor(0xff606060);        } else if (action == MotionEvent.ACTION_UP ||                action == MotionEvent.ACTION_CANCEL) {            v.setBackgroundColor(0);        }        return false;     }    @Override    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfoIn) {        /*         * A better way to do this would be to define a new "media search" intent (which         * would behave similar to a regular search intent), and have amazon, youtube, the         * browser and other suitable apps support it. Then we could just fire off the         * intent and let the user choose from the activity picker.         */        CharSequence title = null;        String query = null;        CharSequence artist = mArtistName.getText();        CharSequence album = mAlbumName.getText();        CharSequence song = mTrackName.getText();        if (view.equals(mArtistName.getParent()) && artist.length() > 0) {            title = artist;            query = artist.toString();        } else if (view.equals(mAlbumName.getParent()) &&                artist.length() > 0 && album.length() > 0) {            title = album ;            query = artist.toString() + " " + album.toString();        } else if (view.equals(mTrackName.getParent()) &&                artist.length() > 0 && song.length() > 0) {            title = song;            query = artist.toString() + " " + song.toString();        } else {            return;        }                title = getString(R.string.mediasearch, title);        TextView tv = new TextView(this);        tv.setText(title);        tv.setTextSize(18);        tv.setPadding(8, 8, 8, 8);        menu.setHeaderView(tv);        //menu.setHeaderTitle(title);                Intent i = new Intent();        i.setAction(Intent.ACTION_SEARCH);        i.setClassName("com.amazon.mp3", "com.amazon.mp3.android.client.SearchActivity");        i.putExtra("query", query);        PackageManager pm = getPackageManager();        ActivityInfo ai = i.resolveActivityInfo(pm, 0);        if ( ai != null) {            menu.add(R.string.mediasearch_amazon).setIntent(i);        }                i = new Intent();        i.setAction(Intent.ACTION_WEB_SEARCH);        i.putExtra("query", query);        menu.add(R.string.mediasearch_google).setIntent(i);        i = new Intent();        i.setAction(Intent.ACTION_SEARCH);        i.setClassName("com.google.android.youtube", "com.google.android.youtube.QueryActivity");        i.putExtra("query", query);        menu.add(R.string.mediasearch_youtube).setIntent(i);        return;    }    private OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {        public void onStartTrackingTouch(SeekBar bar) {            mLastSeekEventTime = 0;        }        public void onProgressChanged(SeekBar bar, int progress, boolean fromtouch) {            if (mService == null) return;            if (fromtouch) {                long now = SystemClock.elapsedRealtime();                if ((now - mLastSeekEventTime) > 250) {                    mLastSeekEventTime = now;                    mPosOverride = mDuration * progress / 1000;                    try {                        mService.seek(mPosOverride);                    } catch (RemoteException ex) {                    }                }            }        }        public void onStopTrackingTouch(SeekBar bar) {            mPosOverride = -1;        }    };        private View.OnClickListener mQueueListener = new View.OnClickListener() {        public void onClick(View v) {            startActivity(                    new Intent(Intent.ACTION_EDIT)                    .setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/Track")                    .putExtra("playlist", "nowplaying")            );        }    };        private View.OnClickListener mShuffleListener = new View.OnClickListener() {        public void onClick(View v) {            toggleShuffle();        }    };    private View.OnClickListener mRepeatListener = new View.OnClickListener() {        public void onClick(View v) {            cycleRepeat();        }    };    private View.OnClickListener mPauseListener = new View.OnClickListener() {        public void onClick(View v) {            doPauseResume();        }    };    private View.OnClickListener mPrevListener = new View.OnClickListener() {        public void onClick(View v) {            if (mService == null) return;            try {                if (mService.position() < 2000) {                    mService.prev();                } else {                    mService.seek(0);                    mService.play();                }            } catch (RemoteException ex) {            }        }    };    private View.OnClickListener mNextListener = new View.OnClickListener() {        public void onClick(View v) {            if (mService == null) return;            try {                mService.next();            } catch (RemoteException ex) {            }        }    };    private RepeatingImageButton.RepeatListener mRewListener =        new RepeatingImageButton.RepeatListener() {        public void onRepeat(View v, long howlong, int repcnt) {            scanBackward(repcnt, howlong);        }    };        private RepeatingImageButton.RepeatListener mFfwdListener =        new RepeatingImageButton.RepeatListener() {        public void onRepeat(View v, long howlong, int repcnt) {            scanForward(repcnt, howlong);        }    };       @Override    public void onStop() {        paused = true;        if (mService != null && mOneShot && getChangingConfigurations() == 0) {            try {                mService.stop();            } catch (RemoteException ex) {            }        }        mHandler.removeMessages(REFRESH);        unregisterReceiver(mStatusListener);        MusicUtils.unbindFromService(this);        super.onStop();    }    @Override    public void onSaveInstanceState(Bundle outState) {        outState.putBoolean("configchange", getChangingConfigurations() != 0);        outState.putBoolean("oneshot", mOneShot);        super.onSaveInstanceState(outState);    }        @Override    public void onStart() {        super.onStart();        paused = false;        if (false == MusicUtils.bindToService(this, osc)) {            // something went wrong            mHandler.sendEmptyMessage(QUIT);        }                IntentFilter f = new IntentFilter();        f.addAction(MediaPlaybackService.PLAYSTATE_CHANGED);        f.addAction(MediaPlaybackService.META_CHANGED);        f.addAction(MediaPlaybackService.PLAYBACK_COMPLETE);        registerReceiver(mStatusListener, new IntentFilter(f));        updateTrackInfo();        long next = refreshNow();        queueNextRefresh(next);    }        @Override    public void onNewIntent(Intent intent) {        setIntent(intent);        mOneShot = intent.getBooleanExtra("oneshot", false);    }        @Override    public void onResume() {        super.onResume();        updateTrackInfo();        setPauseButtonImage();    }        @Override    public void onDestroy()    {        mAlbumArtWorker.quit();        super.onDestroy();        //System.out.println("***************** playback activity onDestroy\n");    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        super.onCreateOptionsMenu(menu);        // Don't show the menu items if we got launched by path/filedescriptor, since        // those tend to not be in the media database.        if (MusicUtils.getCurrentAudioId() >= 0) {            if (!mOneShot) {                menu.add(0, GOTO_START, 0, R.string.goto_start).setIcon(R.drawable.ic_menu_music_library);                menu.add(0, PARTY_SHUFFLE, 0, R.string.party_shuffle); // icon will be set in onPrepareOptionsMenu()            }            SubMenu sub = menu.addSubMenu(0, ADD_TO_PLAYLIST, 0,                    R.string.add_to_playlist).setIcon(R.drawable.ic_menu_add);            MusicUtils.makePlaylistMenu(this, sub);            menu.add(0, USE_AS_RINGTONE, 0, R.string.ringtone_menu_short).setIcon(R.drawable.ic_menu_set_as_ringtone);            menu.add(0, DELETE_ITEM, 0, R.string.delete_item).setIcon(R.drawable.ic_menu_delete);        }        return true;    }    @Override    public boolean onPrepareOptionsMenu(Menu menu) {        MenuItem item = menu.findItem(PARTY_SHUFFLE);        if (item != null) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -