2017-04-04 77 views
1

我目前正在嘗試構建一個包含錄音機的應用程序。雖然這樣做,但我遇到了一個我無法解決的問題,即使我已經搜索了整個互聯網並閱讀了大量的主題。所以這是事情。我創建了一個MediaRecorder的實例。但是,每次嘗試通過OnClick運行.start()方法時,應用程序都會崩潰。我已經在Manifest文件中詢問權限,並通過主要的onCreate()方法手動獲取權限。但是,似乎與此無關。我還看到許多主題告訴人們在.start()之前運行.prepare()方法。但是,在開始研究之前,我已經完成了這項工作。因此,情況並非如此。我在下面添加所有信息。「java.lang.RuntimeException:啓動失敗。」當試圖運行MediaRecorder.start()方法

崩潰日誌:

  --------- beginning of crash 
04-04 17:36:16.631 2605-2605/com.liucveikis.beatboxjam E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.liucveikis.beatboxjam, PID: 2605 
                     java.lang.RuntimeException: start failed. 
                      at android.media.MediaRecorder.start(Native Method) 
                      at com.liucveikis.beatboxjam.SoundRecorderActivity\$1.onClick(SoundRecorderActivity.java:56) 
                      at android.view.View.performClick(View.java:5637) 
                      at android.view.View$PerformClick.run(View.java:22429) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
04-04 17:36:16.632 1612-1983/system_process W/ActivityManager: Force finishing activity com.liucveikis.beatboxjam/.SoundRecorderActivity 

清單包括:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" /> 

手冊許可需求:

 int YOUR_REQUEST_CODE = 200; 

    if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED || 
      ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) //check if permission request is necessary 
      ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.RECORD_AUDIO, android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, YOUR_REQUEST_CODE); 
     } 


    } 

錄音活動:

package com.liucveikis.beatboxjam; 

import android.media.MediaPlayer; 
import android.media.MediaRecorder; 
import android.os.Build; 
import android.os.Environment; 
import android.support.annotation.NonNull; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

import java.io.IOException; 

public class SoundRecorderActivity extends AppCompatActivity { 

    private Button play, stop, record, save; 
    private MediaRecorder audioRecorder; 
    private String outputFile; 
    private boolean permissionToRecordAccepted = false; 
    private boolean permissionToWriteAccepted = false; 
    private String [] permissions = {"android.permission.RECORD_AUDIO", "android.permission.WRITE_EXTERNAL_STORAGE"}; 



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

     play = (Button) findViewById(R.id.playRecord); 
     stop = (Button) findViewById(R.id.stopRecording); 
     record = (Button) findViewById(R.id.startRecording); 
     save = (Button) findViewById(R.id.saveRecord); 

     play.setEnabled(false); 
     stop.setEnabled(false); 
     save.setEnabled(false); 



     outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp"; 
     audioRecorder = new MediaRecorder(); 
     audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
     audioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AAC_ADTS); 
     audioRecorder.setOutputFile(outputFile); 

     record.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       try { 
        audioRecorder.prepare(); 
        audioRecorder.start(); 
       } catch (IllegalStateException e) { 

       } catch (IOException e) { 

       } 

       record.setEnabled(false); 
       stop.setEnabled(false); 

       Toast.makeText(getApplicationContext(), "Recording Started", Toast.LENGTH_LONG).show(); 
      } 
     }); 

     stop.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       audioRecorder.stop(); 
       audioRecorder.release(); 
       audioRecorder = null; 
       stop.setEnabled(false); 
       play.setEnabled(true); 
       Toast.makeText(getApplicationContext(), "Recorded Successfully", Toast.LENGTH_LONG).show(); 
      } 
     }); 

     play.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       MediaPlayer mediaPlayer = new MediaPlayer(); 

       try { 
        mediaPlayer.setDataSource(outputFile); 
        mediaPlayer.prepare(); 
        mediaPlayer.start(); 

        Toast.makeText(getApplicationContext(), "Record Is Playing", Toast.LENGTH_LONG).show(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 

    } 
} 

2017-04-05更改爲錄音活動 - 它仍然不起作用。

package com.liucveikis.beatboxjam; 

import android.media.MediaPlayer; 
import android.media.MediaRecorder; 
import android.os.Build; 
import android.os.Environment; 
import android.support.annotation.NonNull; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

import java.io.IOException; 

public class SoundRecorderActivity extends AppCompatActivity { 

    private static final String LOG_TAG = "LALALALALALALA"; 
    private Button play, stop, record, save; 
    private MediaRecorder audioRecorder; 
    private String outputFile; 
    private boolean permissionToRecordAccepted = false; 
    private boolean permissionToWriteAccepted = false; 
    private String [] permissions = {"android.permission.RECORD_AUDIO", "android.permission.WRITE_EXTERNAL_STORAGE"}; 



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

     play = (Button) findViewById(R.id.playRecord); 
     stop = (Button) findViewById(R.id.stopRecording); 
     record = (Button) findViewById(R.id.startRecording); 
     save = (Button) findViewById(R.id.saveRecord); 

     play.setEnabled(false); 
     stop.setEnabled(false); 
     save.setEnabled(false); 




/*  audioRecorder = new MediaRecorder(); 
     audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
     audioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AAC_ADTS); 
     audioRecorder.setOutputFile(outputFile);*/ 




     record.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 

       outputFile = getFilesDir().getAbsolutePath() + "/recording.3gp"; 
       audioRecorder = new MediaRecorder(); 
       audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
       audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
       audioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); 
       audioRecorder.setOutputFile(outputFile); 

       try { 
        audioRecorder.prepare(); 
       } catch (IOException e) { 
        Log.e(LOG_TAG, "prepare() failed"); 
       } 

       audioRecorder.start(); 

       record.setEnabled(false); 
       stop.setEnabled(true); 

       Toast.makeText(getApplicationContext(), "Recording Started", Toast.LENGTH_LONG).show(); 
      } 
     }); 




     stop.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       audioRecorder.stop(); 
       audioRecorder.release(); 
       audioRecorder = null; 
       stop.setEnabled(false); 
       play.setEnabled(true); 
       Toast.makeText(getApplicationContext(), "Recorded Successfully", Toast.LENGTH_LONG).show(); 
      } 
     }); 




     play.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       MediaPlayer mediaPlayer = new MediaPlayer(); 

       try { 
        mediaPlayer.setDataSource(outputFile); 
        mediaPlayer.prepare(); 
        mediaPlayer.start(); 

        Toast.makeText(getApplicationContext(), "Record Is Playing", Toast.LENGTH_LONG).show(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 



    } 
} 

-------------------編輯:2017年4月17日--------------- ----

好吧,所以我完全重寫了錄音機類。但是,現在我面臨着一個不同的問題。這個東西在Android 6.0(API 23)和Android 7.0(API 24)上完美運行,但在Android 7.1.1(API 25)上崩潰。有沒有關於MediaRecorder的一些變化或一些事情?權限被授予,不需要擔心。

package com.liucveikis.beatboxjam; 


import android.media.MediaPlayer; 
import android.media.MediaRecorder; 
import android.os.Environment; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

import java.io.File; 
import java.io.IOException; 

public class SoundRecorderActivity extends AppCompatActivity { 

    private Button startRecording, stopRecording, playRecord, saveRecord; 
    private MediaPlayer player; 
    private MediaRecorder recorder; 
    private String FILE; 


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

     startRecording = (Button)findViewById(R.id.startRecording); 
     stopRecording = (Button)findViewById(R.id.stopRecording); 
     playRecord = (Button)findViewById(R.id.playRecord); 
     saveRecord = (Button)findViewById(R.id.saveRecord); 

     startRecording.setEnabled(true); 
     stopRecording.setEnabled(false); 
     playRecord.setEnabled(false); 
     saveRecord.setEnabled(false); 

     FILE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tempRecord.3gp"; 


     startRecording.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       try { 
        startRecording(); 
        stopRecording.setEnabled(true); 
        startRecording.setEnabled(false); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 


     stopRecording.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       stopRecording(); 
       stopRecording.setEnabled(false); 
       startRecording.setEnabled(true); 
       playRecord.setEnabled(true); 
      } 
     }); 


     playRecord.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       try { 
        startPlaying(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 


    } 



    public void startRecording() throws IOException { 
     if(recorder!=null){ 
      recorder.stop(); 
      recorder.release(); 
     } 

     File fileOutput = new File(FILE); 
     if(fileOutput!=null){ 
      fileOutput.delete(); 
     } 

     recorder = new MediaRecorder(); 
     recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
     recorder.setOutputFile(FILE); 


     recorder.prepare(); 
     recorder.start(); 

    } 


    public void stopRecording(){ 
     recorder.stop(); 
     recorder.release(); 
    } 

    public void startPlaying() throws IOException { 

     if(player!=null){ 
      player.stop(); 
      player.release(); 
     } 

     player = new MediaPlayer(); 
     player.setDataSource(FILE); 
     player.prepare(); 
     player.start(); 

     player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
      @Override 
      public void onCompletion(MediaPlayer player) { 
       player.release(); 
      } 
     }); 

    } 
} 

錯誤:

04-12 19:46:24.911 1335-1335/? W/APM_AudioPolicyManager: getInputForAttr() failed opening input: samplingRate 8000, format 1, channelMask 10 
04-12 19:46:24.911 1343-1343/? E/AudioRecord: Could not get audio input for session 73, record source 1, sample rate 8000, format 0x1, channel mask 0x10, flags 0 
04-12 19:46:24.911 1343-1343/? E/StagefrightRecorder: audio source is not initialized 
04-12 19:46:24.911 2592-2592/com.liucveikis.beatboxjam E/MediaRecorder: start failed: -2147483648 
04-12 19:46:24.911 2592-2592/com.liucveikis.beatboxjam D/AndroidRuntime: Shutting down VM 


                     --------- beginning of crash 
04-12 19:46:24.912 2592-2592/com.liucveikis.beatboxjam E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.liucveikis.beatboxjam, PID: 2592 
                     java.lang.RuntimeException: start failed. 
                      at android.media.MediaRecorder.start(Native Method) 
                      at com.liucveikis.beatboxjam.SoundRecorderActivity.startRecording(SoundRecorderActivity.java:101) 
                      at com.liucveikis.beatboxjam.SoundRecorderActivity$1.onClick(SoundRecorderActivity.java:45) 
                      at android.view.View.performClick(View.java:5637) 
                      at android.view.View$PerformClick.run(View.java:22429) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
04-12 19:46:24.914 1614-1680/system_process W/ActivityManager: Force finishing activity com.liucveikis.beatboxjam/.SoundRecorderActivity 
+0

你說你的清單包含'CAPTURE_AUDIO_OUTPUT',不應該是'RECORD_AUDIO'?另外看看[這裏](https://developer.android.com/guide/topics/media/mediarecorder.html#example)。 – rckrd

+0

@rckrd在發佈此線程之前,我從清單中刪除了此RECORD_AUDIO行。然而,它在那之前並沒有影響任何東西。無論哪種方式,我都是在第一次啓動的活動中手動請求此權限。我會再次將此行添加到清單,但是,它不會以任何方式工作。 –

+0

@ Karolis.L你找到解決方案嗎?我面臨着與API 25(7.1.1)相同的問題。 –

回答

-1

setContent視圖後,初始化您的媒體播放器。

MediaPlayer mediaplayer = new MediaPlayer();

+0

它是MediaRecorder崩潰,而不是MediaPlayer。 –

相關問題