我正在尝试录音机应用程序,我希望在列表视图中显示它。如果我停止录制,则应将录制的文件添加到ListView中。这应该发生在每一站。但我不知道是否以正确的方式编写代码。我在MainActivity中遇到NullpointerException错误。
这是我的MainActivity代码。
public class MainActivity extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter;
private MediaRecorder myAudioRecorder;
private String outputFile = null;
private Button start,stop,play;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainListView = (ListView) findViewById( R.id.mainListView );
start = (Button)findViewById(R.id.button1);
stop = (Button)findViewById(R.id.button2);
play = (Button)findViewById(R.id.button3);
stop.setEnabled(false);
play.setEnabled(false);
outputFile = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/myrecording.3gp";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(outputFile) );
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
mainListView.setAdapter( listAdapter );
}
public void start(View view){
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
start.setEnabled(false);
stop.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
}
public void stop(View view){
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;
stop.setEnabled(false);
play.setEnabled(true);
Toast.makeText(getApplicationContext(), "Audio recorded successfully",
Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void play(View view) throws IllegalArgumentException,
SecurityException, IllegalStateException, IOException{
MediaPlayer m = new MediaPlayer();
m.setDataSource(outputFile);
m.prepare();
m.start();
Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
//play.setEnabled(toggle);
}
}
这是我的activity_main.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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:text="@string/Recording"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:scaleType="fitXY"
android:src="@drawable/microphone" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_marginTop="67dp"
android:layout_toLeftOf="@+id/imageView1"
android:onClick="start"
android:text="@string/start" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/textView1"
android:layout_marginRight="40dp"
android:onClick="stop"
android:text="@string/stop" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:onClick="play"
android:text="@string/play" />
</RelativeLayout>
这是main_listview.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">
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainListView">
</ListView>
</LinearLayout>
这是我的simplerow.xml代码
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
请纠正我。提前谢谢。
答案 0 :(得分:0)
使用文件实用程序类从存储中获取音频文件
public class ListFilesUtil {
public static List<String> listFiles(String directoryName) {
List<String> fileName = new ArrayList<String>();
directoryName, Context.MODE_PRIVATE).getParentFile();
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + directoryName);
//get all the files from a directory
File[] fList = directory.listFiles();
if (fList != null) {
for (File file : fList) {
if (file.isFile()) {
fileName.add(file.getName());
}
}
}
return fileName;
}
public static List<String> listFolders( String directoryName) {
List<String> fileName = new ArrayList<String>();
directoryName, Context.MODE_PRIVATE).getParentFile();
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + directoryName);
//get all the files from a directory
File[] fList = directory.listFiles();
if (fList != null) {
for (File file : fList) {
if (file.isDirectory()) {
fileName.add(file.getName());
}
}
}
Collections.sort(fileName);
return fileName;
}
}