Android Media Player

眉间皱痕 提交于 2019-12-11 16:47:26

问题


I do simple small programs in android for my practice. While I am on the way to create a simple player app, I had to face an error which I could not solve.The following is my code.

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AndrmusiActivity extends Activity {
    /** Called when the activity is first created. */
    public MediaPlayer playr;
    public Button b1;
    public Button b2;
    public Button b3;
    @Override
    public void onCreate(Bundle State) {
        super.onCreate(State);
        setContentView(R.layout.main);
        b1= (Button)findViewById(R.id.play);
        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {                   
            playr = MediaPlayer.create(this,R.raw.showme);
            playr.start();

            }
        });
        b2= (Button)findViewById(R.id.Pause);           
        b2.setOnClickListener(new OnClickListener() {           
            public void onClick(View v1) {                  
                playr.pause();              
            }
        });
        b3=(Button)findViewById(R.id.Stop);            
        b3.setOnClickListener(new OnClickListener() {               
            public void onClick(View v) {                   
                playr.stop();
                playr.reset();

            }
        });

    }
}

Now I faced the error at play method at the line

playr = MediaPlayer.create(this,R.raw.showme);

Could any one please help me in this aspect. Thanks in adv


回答1:


Use this line there

 playr = MediaPlayer.create(AndrmusiActivity.this,R.raw.showme);

If error remains post your error logcat.

create this in your on create() method.

 playr = MediaPlayer.create(this,R.raw.showme);

For resume()

use your your code as same as below.

b1 = (Button) findViewById(R.id.play);
b1.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             playr.start();
         }
     });

Calling start() to resume playback for a paused MediaPlayer object, and the resumed playback position is the same as where it was paused. When the call to start() returns, the paused MediaPlayer object goes back to the Started state.

http://developer.android.com/reference/android/media/MediaPlayer.html#start()




回答2:


Replace

playr = MediaPlayer.create(this,R.raw.showme);

with

playr = MediaPlayer.create(AndrmusiActivity.this,R.raw.showme);



回答3:


use this code to create mediaplayer context

 playr = MediaPlayer.create(getBaseContext(),R.raw.showme);



回答4:


This is my activity example to play videos in Android using VideoView:

package br.com.player;

import android.support.v7.app.AppCompatActivity;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;

public class PlayerActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);

        Button buttonPlayVideo = (Button)findViewById(R.id.playvideobuttom);

        getWindow().setFormat(PixelFormat.UNKNOWN);

        VideoView mVideoView = (VideoView)findViewById(R.id.videoview);

        buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {

                    VideoView mVideoView = (VideoView)findViewById(R.id.videoview);

                    String uriPath = "android.resource://br.com.player/"+R.raw.filevideo;

                    Uri uri = Uri.parse(uriPath);
                    mVideoView.setVideoURI(uri);
                    mVideoView.requestFocus();
                    mVideoView.start();

            }});
     }
}

And this is activity's xml player.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

  <Button
    android:id="@+id/playvideobuttom" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Play Video"
    />

   <VideoView
    android:id="@+id/videoview" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

</LinearLayout>


来源:https://stackoverflow.com/questions/8787509/android-media-player

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!