簡體   English   中英

Android 錯誤方法 MediaPlayer.create( ) 不適用

[英]Android Error method MediaPlayer.create( )is not applicable

我正在嘗試制作一個帶有播放和停止按鈕的簡單媒體播放器。 但我收到以下錯誤:
方法MediaPlayer.create(Context, int)不適用,
方法MediaPlayer.create(Context, Uri, SurfaceHolder)不適用,
方法MediaPlayer.create(Context, Uri)不適用。

我看過一些關於 StakeOverflow 的帖子,但似乎都沒有幫助。 任何人都可以啟發我嗎?

以下是我發現的一些:

上下文無法識別:方法不適用於參數

無法在 Android 應用程序的 new View.OnFocusChangeListener() 中使用 MediaPlayer.create

public class MainFragment extends Fragment {

private MediaPlayer mediaPlayer;
public TextView songName, duration;
private double timeElapsed = 0, finalTime = 0;
private Handler durationHandler = new Handler();
private SeekBar seekbar;

public MainFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //initialize views
    initializeViews();
}

public void initializeViews() {
    songName = (TextView) getView().findViewById(R.id.songName);
    mediaPlayer = MediaPlayer.create(this, R.raw.no_diggity);
    finalTime = mediaPlayer.getDuration();
    duration = (TextView) getView().findViewById(R.id.songDuration);
    seekbar = (SeekBar) getView().findViewById(R.id.seekBar);
    //songName.setText("Sample_Song.mp3");

    seekbar.setMax((int) finalTime);
    seekbar.setClickable(false);
}

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    return rootView;
}

// play mp3 song
public void play(View view) {
    mediaPlayer.start();
    timeElapsed = mediaPlayer.getCurrentPosition();
    seekbar.setProgress((int) timeElapsed);
    durationHandler.postDelayed(updateSeekBarTime, 100);
}

//handler to change seekBarTime
private Runnable updateSeekBarTime = new Runnable() {
    public void run() {
        //get current position
        timeElapsed = mediaPlayer.getCurrentPosition();
        //set seekbar progress
        seekbar.setProgress((int) timeElapsed);
        //set time remaing
        double timeRemaining = finalTime - timeElapsed;
        duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));

        //repeat yourself that again in 100 miliseconds
        durationHandler.postDelayed(this, 100);
    }
};
}

您將片段作為上下文傳遞,而是傳遞活動:

mediaPlayer = MediaPlayer.create(this.getActivity(), R.raw.no_diggity);

然后不要使用 onCreate 方法,把你的 initializeViews(); 在加載視圖之后和返回之前調用 onCreateView() 方法

@Override
public View onCreateView(LayoutInflater inflater,
                     ViewGroup container,
                     Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    initializeViews();
    return rootView;
}

編輯嘗試這樣的事情,我只是在這里復制/粘貼你的代碼,它無法工作,它只是一個關於事情應該如何工作的想法:

 public void initializeViews(View view) {
    //HERE YOU GET THE INSTANCES OF THE VIEWS IN THE LAYOUT
    songName = (TextView)view.findViewById(R.id.songName);
    mediaPlayer = MediaPlayer.create(this.getActivity(), R.raw.no_diggity);
    finalTime = mediaPlayer.getDuration();
    duration = (TextView)view.findViewById(R.id.songDuration);
    seekbar = (SeekBar)view.findViewById(R.id.seekBar);
    //songName.setText("Sample_Song.mp3");

    seekbar.setMax((int) finalTime);
    seekbar.setClickable(false);
}

@Override
public View onCreateView(LayoutInflater inflater,
                     ViewGroup container,
                     Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    initializeViews(rootView)

    return rootView;
}

我的建議是,您首先要尋找一些有關如何使用片段的代碼示例,因為您缺少一些基礎知識,在使用 MediaPlayer 的工作示例之后,互聯網上應該有很多。 希望能幫助到你。

EDIT2 片段上媒體播放器的工作示例:

活動布置:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" />

片段布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Playing Music from Resources"
        android:textSize="20dp" />

    <Button
        android:id="@+id/play"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:text="Play" />

    <Button
        android:id="@+id/stop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:text="Stop" />

</LinearLayout>

活動代碼:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.container, new MyFragment()).commit();
        }
    }
}

片段代碼:

public class MyFragment extends Fragment {

    MediaPlayer mPlayer;
    Button buttonPlay;
    Button buttonStop;

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.my_fragment, container, false);

        buttonPlay = (Button)rootView.findViewById(R.id.play);
        buttonPlay.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Uri myUri = Uri.parse("android.resource://com.example.player/" + R.raw.music);
                mPlayer = new MediaPlayer();
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    mPlayer.setDataSource(MyFragment.this.getActivity(), myUri);
                } catch (IllegalArgumentException e) {
                    Toast.makeText(MyFragment.this.getActivity(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (SecurityException e) {
                    Toast.makeText(MyFragment.this.getActivity(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IllegalStateException e) {
                    Toast.makeText(MyFragment.this.getActivity(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    mPlayer.prepare();
                } catch (IllegalStateException e) {
                    Toast.makeText(MyFragment.this.getActivity(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    Toast.makeText(MyFragment.this.getActivity(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                }
                mPlayer.start();
            }
        });

        buttonStop = (Button)rootView.findViewById(R.id.stop);
        buttonStop.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(mPlayer!=null && mPlayer.isPlaying()){
                    mPlayer.stop();
                }
            }
        });

        return rootView;
    }
}

這是工作和測試,我從這里拿了代碼,音樂播放在原始文件夾中。 您想要添加為搜索欄的其他內容,只需了解如何將其包含在此處即可。 但這是最基本的。 希望你能解決這個問題。

使用您的原始發布代碼,刪除該行

mediaPlayer = MediaPlayer.create(this, R.raw.no_diggity);

從 initializeViews() 方法和

做這個:

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    mediaPlayer = MediaPlayer.create(getActivity(), R.raw.no_diggity);
    mediaPlayer.start();
    initializeViews();
    return rootView;
}

這個問題已經在另一篇文章中回答了:

Android:媒體播放器創建

問題在於聲音文件擴展名或文件類型。

例如,如果您有sound.mp3就不會出現問題,或者如果您在 raw 文件夾中有sound.wavsound.WAV出錯,所以我認為某些版本的 android studio 不支持 wav 文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM