媒体播放器中没有控制按钮显示

时间:2013-04-13 09:35:38

标签: android controller android-mediaplayer media-player media

我是Android新手,我正在尝试创建一个非常简单的应用。它只是一个包含用于播放音频或视频文件的MediaPlayer的活动。 我设法播放了一首歌,但它不会向我展示媒体播放器的控制元素!

以下是我正在使用的代码:

public class Player extends Activity implements OnPreparedListener,
        MediaPlayerControl {

    private static final String TAG = "AudioPlayer";
    public static final String AUDIO_FILE_NAME = "audioFileName";

    private String audioFile;
    private MediaPlayer mediaPlayer;
    private Handler handler = new Handler();
    private MediaController mediaController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);



        Uri myUri = Uri.parse("android.resource://com.example.mediaplayer/"
                + R.raw.lied); // initialize Uri here
        MediaPlayer mediaPlayer = new MediaPlayer();

        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaController = new MediaController(this);
        try {
            mediaPlayer.setDataSource(getApplicationContext(), myUri);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            mediaPlayer.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mediaPlayer.start();

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.player, menu);
        return true;
    }

    public void onPrepared(MediaPlayer mediaPlayer) {
        Log.d(TAG, "onPrepared");
        mediaController.setMediaPlayer(this);
        mediaController.setAnchorView(findViewById(R.id.mediaController1));

        handler.post(new Runnable() {
            public void run() {
                mediaController.setEnabled(true);
                mediaController.show();
            }
        });
    }

    protected void onStop() {
        super.onStop();
        mediaPlayer.stop();
        mediaPlayer.release();
    }

    public boolean onTouchEvent(MotionEvent event) {
        // the MediaController will hide after 3 seconds - tap the screen to
        // make it appear again
        mediaController.show();
        return false;
    }

    public boolean canPause() {

        return true;
    }

    public boolean canSeekBackward() {

        return true;
    }

    public boolean canSeekForward() {

        return true;
    }

    public int getBufferPercentage() {

        return 0;
    }

    public int getCurrentPosition() {
        return mediaPlayer.getCurrentPosition();
    }

    public int getDuration() {
        return mediaPlayer.getDuration();

    }

    public boolean isPlaying() {
        return mediaPlayer.isPlaying();

    }

    public void pause() {
        mediaPlayer.pause();

    }

    public void seekTo(int i) {
        mediaPlayer.seekTo(i);

    }

    public void start() {
        mediaPlayer.start();

    }
}

这是我的布局XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Player" >

    <MediaController
        android:id="@+id/mediaController1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="122dp"
        android:layout_marginTop="55dp"
        android:visibility="visible" >

    </MediaController>

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

我认为您所寻找的是Android中的视频观看。它在播放媒体时有默认控件。

在XML布局文件夹中创建一个视频视图,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
<VideoView
   android:id="@+id/myvideoview"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content" />
</LinearLayout>

假设您调用了该文件main.xml

现在创建一个名为MediaViewerTest.java的活动,如下所示:

public class MediaViewerTest extends Activity {

String SrcPath = http://myMediaFile.mp3"; // or however your getting your media source, im assuming web here

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
       myVideoView.setVideoURI(Uri.parse(SrcPath));
       myVideoView.setMediaController(new MediaController(this));
       myVideoView.requestFocus();
       myVideoView.start();
   }
}