使用C语言中的system()播放多个音频文件

时间:2016-03-30 14:13:01

标签: c audio

我已经编写了一个C代码,使用vlc一个接一个地播放3个音频文件,但是在播放了第一个文件之后它没有继续我按Ctrl + C或q转到下一首我想发生的歌。 我在每个文件后放置了系统(" q"),以便它可以完成我的任务,但它仍然无法正常工作。

#include<stdio.h>

int main(){
    system("vlc 1.mp3");
    system("q");
    system("vlc 2.mp3");
    system("q");
    system("vlc 3.mp3");
    system("q");    
    return 0;
} 

2 个答案:

答案 0 :(得分:0)

我认为你应该在slave模式下使用mplayer而不是vlc。它更灵活,更有控制力。您可以根据需要向mplayer发送命令。请学习以下链接

http://www.mplayerhq.hu/DOCS/tech/slave.txt

我建议你使用python for linux和C#或VB.NET for windows。如果需要,我可以提供一些vb.net代码。

答案 1 :(得分:0)

这是我对另一个问题的旧答案。

但我也会在这里发帖。

我正在开发android手机远程控制+ VB.NET TCP服务器 - mplayer。我在奴隶模式下使用mplayer。我从android app发送命令到VB.NET TCP服务器。然后命令将发送到mplayer。

我将显示一些控制和发送mplayer所需命令的代码,但服务器部分尚未完成。编码还没有完成,但我希望它对你有用。

Imports System.ComponentModel
Imports System.IO
Imports System.Data.OleDb
    Public Class Form1
      Private bw As BackgroundWorker = New BackgroundWorker
      Dim i As Integer = 0
      Dim dbFile As String = Application.StartupPath & "\Data\Songs.accdb"
      Public connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & dbFile & "; persist security info=false"
      Public conn As New OleDbConnection(connstring)
      Dim sw As Stopwatch
      Dim ps As Process = Nothing
      Dim jpgPs As Process = Nothing
      Dim args As String = Nothing
      Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
         Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)

         If bw.CancellationPending = True Then
             e.Cancel = True
             Exit Sub
         Else
        ' Perform a time consuming operation and report progress.
        'System.Threading.Thread.Sleep(500)
        bw.ReportProgress(i * 10)
        Dim dir_info As New DirectoryInfo(TextBox1.Text)
        ListFiels("SongList", TextBox2.Text, dir_info)
    End If

 End Sub

   Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
    If e.Cancelled = True Then
        Me.tbProgress.Text = "Canceled!"
    ElseIf e.Error IsNot Nothing Then
        Me.tbProgress.Text = "Error: " & e.Error.Message
    Else
        Me.tbProgress.Text = "Done!"
    End If
End Sub

  Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
       Me.tbProgress.Text = e.ProgressPercentage.ToString() & "%"
  End Sub

  Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Try
           ps.Kill()
        Catch
            Debug.Write("already closed")
        End Try
    End Sub


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False 'To avoid error from backgroundworker
    bw.WorkerReportsProgress = True
    bw.WorkerSupportsCancellation = True
    AddHandler bw.DoWork, AddressOf bw_DoWork
    AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
    AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
    funPlayMusic()
End Sub

Private Sub buttonStart_Click(sender As Object, e As EventArgs) Handles buttonStart.Click
    If Not bw.IsBusy = True Then
        bw.RunWorkerAsync()
    End If
End Sub

Private Sub buttonCancel_Click(sender As Object, e As EventArgs) Handles buttonCancel.Click
    If bw.WorkerSupportsCancellation = True Then
        bw.CancelAsync()
    End If
End Sub

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    If Not bw.IsBusy = True Then
        sw = Stopwatch.StartNew()
        bw.RunWorkerAsync()
        sw.Stop()
        Label1.Text = ":   " + sw.Elapsed.TotalMilliseconds.ToString() + " ms"
    End If
End Sub
Private Sub ListFiels(ByVal tblName As String, ByVal pattern As String, ByVal dir_info As DirectoryInfo)
    i = 0
    Dim fs_infos() As FileInfo = Nothing
    Try
        fs_infos = dir_info.GetFiles(pattern)
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try


    For Each fs_info As FileInfo In fs_infos
        i += 1
        Label1.Text = i
        insertData(tblName, fs_info.FullName)
        lstResults.Items.Add(i.ToString() + ":" + fs_info.FullName.ToString())
        If i = 1 Then
            Playsong(fs_info.FullName.ToString())
        Else
            i = 0
            lstResults.Items.Clear()
        End If
    Next fs_info
    sw.Stop()
    Label1.Text = ":   " + sw.Elapsed.TotalMilliseconds.ToString() + " ms"
    fs_infos = Nothing

    Dim subdirs() As DirectoryInfo = dir_info.GetDirectories()
    For Each subdir As DirectoryInfo In subdirs
        ListFiels(tblName, pattern, subdir)
    Next

End Sub
Private Sub insertData(ByVal tableName As String, ByVal foundfile As String)
    Try
        If conn.State = ConnectionState.Open Then conn.Close()
        conn.Open()

        Dim SqlQuery As String = "INSERT INTO " & tableName & " (SngPath) VALUES (@sng)"
        Dim SqlCommand As New OleDbCommand
        With SqlCommand
            .CommandType = CommandType.Text
            .CommandText = SqlQuery
            .Connection = conn
            .Parameters.AddWithValue("@sng", foundfile)
            .ExecuteNonQuery()
        End With

        conn.Close()
    Catch ex As Exception
        conn.Close()
        MsgBox(ex.Message)
    End Try
End Sub


Private Sub btnClearList_Click(sender As Object, e As EventArgs) Handles btnClearList.Click
    lstResults.Items.Clear()
End Sub

Private Sub funPlayMusic()

    ps = New Process()
    ps.StartInfo.FileName = "D:\Music\mplayer.exe "
    ps.StartInfo.UseShellExecute = False
    ps.StartInfo.RedirectStandardInput = True
    jpgPs = New Process()
    jpgPs.StartInfo.FileName = "D:\Music\playjpg.bat"
    jpgPs.StartInfo.UseShellExecute = False
    jpgPs.StartInfo.RedirectStandardInput = True
    'ps.StartInfo.CreateNoWindow = True
    args = "-fs  -noquiet -identify -slave " '
    args += "-nomouseinput -sub-fuzziness 1 "
    args += " -vo direct3d, -ao dsound "
    '    -wid will tell MPlayer to show output inisde our panel
    '    args += " -vo direct3d, -ao dsound  -wid ";
    '    int id = (int)panel1.Handle;
    '    args += id;
End Sub
Public Function SendCommand(ByVal cmd As String) As Boolean
    Try
        If ps IsNot Nothing AndAlso ps.HasExited = False Then
            ps.StandardInput.Write(cmd + vbLf)
            'MessageBox.Show(ps.StandardOutput.ReadToEndAsync.ToString())
            Return True
        Else
            Return False
        End If

    Catch ex As Exception
        Return False
    End Try
End Function

Public Sub Playsong(ByVal Songfilelocation As String)

    Try
        ps.Kill()
    Catch
    End Try
    Try
        ps.StartInfo.Arguments = args + " """ + Songfilelocation + """"
        ps.Start()

        SendCommand("set_property volume " + "80")
    Catch e As Exception
        MessageBox.Show(e.Message)
    End Try

End Sub

Private Sub lstResults_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstResults.SelectedIndexChanged
    Playsong(lstResults.SelectedItem.ToString())
End Sub



Private Sub btnPlayJPG_Click(sender As Object, e As EventArgs) Handles btnPlayJPG.Click
    Try
        ' jpgPs.Kill()
    Catch
    End Try
    Try
        'ps.StartInfo.Arguments = "–fs –mf fps=5 mf://d:/music/g1/Image00020.jpg –loop 200" '-vo gl_nosw
        'jpgPs.Start()
        Shell("d:\Music\playjpg.bat")

        ' SendCommand("set_property volume " + "80")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub


Private Sub btnPlayPause_Click(sender As Object, e As EventArgs) Handles btnPlayPause.Click
    SendCommand("pause")

End Sub

Private Sub btnMute_Click(sender As Object, e As EventArgs) Handles btnMute.Click
    SendCommand("mute")
End Sub

Private Sub btnKaraoke_Click(sender As Object, e As EventArgs) Handles btnKaraoke.Click
    'SendCommand("panscan 0-0 | 1-1")
    SendCommand("af_add pan=2:1:1:0:0")


End Sub

Private Sub btnStereo_Click(sender As Object, e As EventArgs) Handles btnStereo.Click
    SendCommand("af_add pan=2:0:0:1:1")
End Sub

Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
    Playsong("d:\music\iot.mp4")
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'SendCommand("loadfile d:\music\iot.mp4")
    'SendCommand("pt_step 1")

End Sub

End Class